#!/bin/bash

R="\\033[1;31m"
V="\\033[1;32m"
J="\\033[1;33m"
N="\\033[0;39m"

if [ $USER != "root" ]; then
	echo -n "$R"
	echo -n "Doit être lancé en tant que root."
	echo "$N"
	exit 1
fi

CUR_DIR=`pwd`

LOG_FILE=$CUR_DIR/phpcs-install-`date +"%Y%m%d%H%M%S"`.log

touch $LOG_FILE

sudo apt-get install -y php5-cli php-pear &>> $LOG_FILE

# Installation de PHP CodeSniffer
echo -n "Installation de PHP CodeSniffer : "

# Création du dossier principal si besoin.
if [ ! -d "/opt/php-factory" ]; then
        mkdir /opt/php-factory
fi

cd /opt/php-factory

tar -xzf $CUR_DIR/repository/PHP_CodeSniffer-1.2.2.tar.gz PHP_CodeSniffer-1.2.2 &>> $LOG_FILE

if [ "$?" = 0 ]; then
	echo -e "$V OK $N"
else
	echo -e "$R KO $N"
	exit 1
fi 

sed -i "s#@php_bin@#`which php`#" /opt/php-factory/PHP_CodeSniffer-1.2.2/scripts/phpcs

sudo chown --recursive www-data:www-data /opt/php-factory/PHP_CodeSniffer-1.2.2

ln -s /opt/php-factory/PHP_CodeSniffer-1.2.2/scripts/phpcs /usr/bin/phpcs

# todo: Créer une interface web à ce produit et l'ajouter au backoffice.

